/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chimos.property.compiler.passtwo.javafx;

import static org.objectweb.asm.Opcodes.ASM4;
import static org.objectweb.asm.Opcodes.GETFIELD;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.PUTFIELD;

import java.util.Map;

import org.objectweb.asm.MethodVisitor;

import de.chimos.property.compiler.Config;
import de.chimos.property.compiler.PropertyInfo;

/**
 * @author Niklas Hofmann
 */
public class PassTwoJFXMethodVisitor extends MethodVisitor {
    
    private final String className;
    private final String methodName;
    
    private final Map<String, Map<String, PropertyInfo>> properties;
    
    private int adjustCounter = 0;
    
    @SuppressWarnings("unused")
	private final Config config;
    
    public PassTwoJFXMethodVisitor(String className, String methodName, Map<String, Map<String, PropertyInfo>> properties, MethodVisitor mv, Config config)
    {
        super(ASM4, mv);
        
        this.className = className;
        this.methodName = methodName;
        
        this.properties = properties;
        
        this.config = config;
    }

    @Override
    public void visitFieldInsn(int opcode, String owner, String name, String desc)
    {
        if(properties.containsKey(owner) && properties.get(owner).containsKey(name))
        {
            PropertyInfo property = properties.get(owner).get(name);
            
            if(opcode == GETFIELD)
            {
                mv.visitMethodInsn
                        (
                            INVOKEVIRTUAL,
                            property.className,
                            className.equals(property.className)?
                                property.internalGetterMethodName:
                                property.getterMethodName,
                            "()"+property.dataSignature
                        );
                ++adjustCounter;
            }
            else if(opcode == PUTFIELD)
            {
                mv.visitMethodInsn
                        (
                            INVOKEVIRTUAL,
                            property.className,
                            className.equals(property.className)?
                                property.internalSetterMethodName:
                                property.setterMethodName,
                            "("+property.dataSignature+")V"
                        );
                ++adjustCounter;
            }
            else
            {
                super.visitFieldInsn(opcode, owner, name, desc);
            }
        }
        else
        {
            super.visitFieldInsn(opcode, owner, name, desc);
        }
    }

	@Override
	public void visitEnd() {
		if(adjustCounter > 0)
		{
			System.out.println("Adjusted "+adjustCounter+" instructions in "+className+"/"+methodName+"()");
		}
		super.visitEnd();
	}
    
    
    
}
